﻿using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace ExcelAutomator.Validation
{
	public class ValidationEngine
	{
		private readonly IList<IValidationResult> m_Results = new List<IValidationResult>();

		#region Validate

		public virtual ValidationEngine Validate
		{
			get
			{
				return this;
			}
		}

		#endregion

		#region That

		public ValidationEngine That
		{
			get { return this; }
		}

		#endregion

		#region Xml(XDocument xDocument)

		public virtual XmlValidator Xml(XDocument xDocument)
		{
			var xmlVaidator = new XmlValidator(xDocument);
			this.Add(xmlVaidator);
			return xmlVaidator;
		}

		#endregion

		#region String(string value, string fieldname)

		public virtual StringValidator String(string value, string fieldname)
		{
			var stringValidator = new StringValidator(value, fieldname);
			this.Add(stringValidator);
			return stringValidator;
		}

		#endregion

		#region IO

		public IoValidation IO
		{
			get
			{
				return new IoValidation(this);
			}
		}

		#endregion

		public virtual ReflectionValidator Reflected(object validationObject)
		{
			var validator = new ReflectionValidator(validationObject, this);
			return validator;
		}

		public ValidationEngine SetCustomViolationMessage(string message)
		{
			var customValidator = new CustomViolationMessageCreator(message);
			Add(customValidator);
			return this;
		}

		public bool IsValid
		{
			get { return this.GenerateReport().IsValid; }
		}

		public virtual void Add(IValidationResult validationResult)
		{
			m_Results.Add(validationResult);
		}

		#region GenerateReport

		public ValidationReport GenerateReport()
		{
			return new ValidationReport(m_Results.ToList());
		}

		#endregion

		#region Reset

		public void Reset()
		{
			m_Results.Clear();
		}

		#endregion
	}
}
